package chenji.moreitemfunapi.textures.plm;

import org.bukkit.entity.Player;

import com.google.common.base.Charsets;

import chenji.moreitemfunapi.MifAPI;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class PluginMessageManager {
	public static final String CHANNELNAME = "mifhelper";

	public void setGetTexturePackage(String name, Player p) {
		byte[] namebytes = name.getBytes(Charsets.UTF_8);
		ByteBuf buf = Unpooled.buffer();
		buf.writeBoolean(true);
		buf.writeInt(namebytes.length);
		buf.writeBytes(namebytes);
		p.sendPluginMessage(MifAPI.getPlugin(MifAPI.class), CHANNELNAME, buf.array());
	}

	public void sendHeadPack(Player p,int amount){
		ByteBuf buf = Unpooled.buffer();
		buf.writeBoolean(false);
		buf.writeBoolean(false);
		buf.writeInt(amount);
		p.sendPluginMessage(MifAPI.getPlugin(MifAPI.class), CHANNELNAME, buf.array());
	}
	public void sendPack(Player p,int id,byte[] data){
		ByteBuf buf = Unpooled.buffer(32000);
		buf.writeBoolean(false);
		buf.writeBoolean(true);
		buf.writeInt(id);
		buf.writeInt(data.length);
		buf.writeBytes(data);
		p.sendPluginMessage(MifAPI.getPlugin(MifAPI.class), CHANNELNAME, buf.array());
	}

	public void receivePackage(byte[] data, Player p) {
		ByteBuf buf = Unpooled.copiedBuffer(data);
		int state = buf.readInt();
		if (state == 3) {
			MifAPI.getTexturesUpdateManager().dealStateThree(p, buf);
		} else {
			MifAPI.getTexturesUpdateManager().dealOtherPackage(state, p);
		}
	}

}
